package cn.dy.workatten.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	/**
	 * 获得日期的年份
	 * 
	 * @param date
	 * @return
	 */
	public static int getDateYear(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int year = c.get(Calendar.YEAR);
		return year;
	}

	/**
	 * 获得日期的月份
	 * 
	 * @param date
	 * @return
	 */
	public static int getDateMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int month = c.get(Calendar.MONTH) + 1;
		return month;
	}

	/**
	 * 获得日期的月份的第几天
	 * 
	 * @param date
	 * @return
	 */
	public static int getDateDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int day = c.get(Calendar.DAY_OF_MONTH);
		return day;
	}

	/**
	 * 获得某个日期的月份开始时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getMonthStartTime(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_MONTH, 1);
		Date startTime = c.getTime();
		startTime.setHours(0);
		startTime.setMinutes(0);
		startTime.setSeconds(0);
		return startTime;
	}

	/**
	 * 获得某个日期的月份结束时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getMonthEndTime(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		// 设置日期为本月最大日期
		c.set(Calendar.DATE, c.getActualMaximum(c.DATE));
		Date endTime = c.getTime();
		endTime.setHours(23);
		endTime.setMinutes(59);
		endTime.setSeconds(59);
		return endTime;
	}

	/**
	 * 获得某个日期是星期几
	 * 
	 */
	public static int getDayForWeek(Date date) throws Exception {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int dayForWeek = 0;
		if (c.get(Calendar.DAY_OF_WEEK) == 1) {
			dayForWeek = 7;
		} else {
			dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
		}
		return dayForWeek;
	}

	public static void main(String[] args) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println(sf.format(getMonthStartTime(new Date())));
		System.out.println(sf.format(getMonthEndTime(new Date())));
	}

}
